package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.extension.UserExtensionData;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.entity.extension.UserExtensionKeyEnum;


import java.sql.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserExtensionDaoImpl extends TempDataObjectDao<UserExtensionData> {

    public static String getUserExtensionDataSql = "select * from `t_u_extension` where `userId`=? and `extensionKey`=?";

    public UserExtensionData getUserExtensionData(long userId, UserExtensionKeyEnum extensionKey) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserExtensionDataSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, extensionKey.getValue());
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserExtensionDataSql = "update `t_u_extension` set intValue=?,stringValue=?,createAt=?,updateAt=? where userId=? and extensionKey=?";

    @Override
    protected boolean updateData(UserExtensionData userExtensionData) {
        userExtensionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, userExtensionData.getIntValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, userExtensionData.getStringValue()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userExtensionData.getCreateAt().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userExtensionData.getUpdateAt().getTime())));
        para.put(i++, new DbParameter(Types.BIGINT, userExtensionData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userExtensionData.getExtensionKey().getValue()));
        if (getDb().executeNoneQuery(updateUserExtensionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserExtensionDataSql = "insert into `t_u_extension`(userId,extensionKey,intValue,stringValue,createAt,updateAt) values (?,?,?,?,?,?)";

    @Override
    protected boolean addData(UserExtensionData userExtensionData) {
        userExtensionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userExtensionData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, userExtensionData.getExtensionKey().getValue()));
        para.put(i++, new DbParameter(Types.INTEGER, userExtensionData.getIntValue()));
        para.put(i++, new DbParameter(Types.VARCHAR, userExtensionData.getStringValue()));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userExtensionData.getCreateAt().getTime())));
        para.put(i++, new DbParameter(Types.TIMESTAMP, new Timestamp(userExtensionData.getUpdateAt().getTime())));
        if (getDb().executeNoneQuery(insertUserExtensionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserExtensionData parseData(ResultSet rs) throws SQLException {
        UserExtensionData userExtensionData = new UserExtensionData();
        userExtensionData.setUserId(rs.getLong("userId"));
        int i = rs.getInt("extensionKey");
        UserExtensionKeyEnum extensionKeyEnum = Arrays.stream(UserExtensionKeyEnum.values())
                .filter(v -> v.getValue() == i).findFirst().orElse(UserExtensionKeyEnum.UNKNOWN_ENUM);
        userExtensionData.setExtensionKey(extensionKeyEnum);
        userExtensionData.setIntValue(rs.getInt("intValue"));
        userExtensionData.setStringValue(rs.getString("stringValue"));
        userExtensionData.setCreateAt(rs.getTimestamp("createAt"));
        userExtensionData.setUpdateAt(rs.getTimestamp("updateAt"));

        return userExtensionData;
    }

    public Map<UserExtensionKeyEnum, UserExtensionData> getUserExtensionDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<UserExtensionKeyEnum, UserExtensionData> userExtensionDataMap = new ConcurrentHashMap<>();
        String selectSql = "select * from t_u_extension where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserExtensionData userExtensionData = parse(rs);
                userExtensionDataMap.put(userExtensionData.getExtensionKey(), userExtensionData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return userExtensionDataMap;
    }

}
